package cn.edu.tsinghua.hpc.gsmsensor;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MyMapView extends MapActivity {
	private static final String TAG = "MapActivity";
	private Spinner spinner;
	private MapView mMapView;
	private MapController mc;
	List<Overlay> mMapOverlays;
	MyItemizedOverlay mItemizedoverlay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_view);
		// initSpinner();
		initMap();

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (this.getIntent().getExtras().containsKey("lat")
				&& this.getIntent().getExtras().containsKey("lon")) {

			int latE6 = (int) (this.getIntent().getExtras().getDouble("lat") * 1E6);
			int lonE6 = (int) (this.getIntent().getExtras().getDouble("lon") * 1E6);
			Log.d(TAG, latE6 + " " + lonE6);
			setMark(latE6, lonE6);
		}
	}

	private void initMap() {
		mMapView = (MapView) findViewById(R.id.map);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setSatellite(false);
		mMapView.setTraffic(false);
		mMapOverlays = mMapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
		mItemizedoverlay = new MyItemizedOverlay(drawable, mMapView);
		mc = mMapView.getController();
		mc.setZoom(20);

		spinner = (Spinner) this.findViewById(R.id.spinner1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item);

		adapter.add(getString(R.string.map));
		adapter.add(getString(R.string.satellite));

		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(selectListener);
	}

	private OnItemSelectedListener selectListener = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView parent, View v, int position,
				long id) {
			if (((String) parent.getItemAtPosition(position))
					.equals(getString(R.string.map))) {
				mMapView.setSatellite(false);
			} else {
				mMapView.setSatellite(true);
			}
		}

		public void onNothingSelected(AdapterView arg0) {
		}
	};

	private void setMark(int lat, int lon) {
		GeoPoint point = new GeoPoint(lat, lon);
		OverlayItem overlayitem = new OverlayItem(point, "Hi!", "Say something");
		mItemizedoverlay.addOverlay(overlayitem);
		mMapOverlays.add(mItemizedoverlay);
		mc.animateTo(point);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
